package com.ruoyi.web.controller.epidemic;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.epidemic.domain.EpidemicUserDept;
import com.ruoyi.epidemic.domain.WxCpSysUser;
import com.ruoyi.epidemic.domain.WxCpSysUserBatch;
import com.ruoyi.epidemic.service.IEpidemicControlDeptService;
import com.ruoyi.epidemic.service.IEpidemicWriteDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理【基础用户-填报】
 */
@Controller
@RequestMapping("/epidemic/writeuser")
public class WriteUserController extends BaseController {

    private String prefix = "epidemic/user/write";

    @Autowired
    private IEpidemicWriteDeptService epidemicWriteDeptService;

    @Autowired
    private IEpidemicControlDeptService epidemicControlDeptService;

    @RequiresPermissions("epidemic:writeuser:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/user";
    }

    /**
     * 填报用户列表
     * @param wxCpSysUser
     * @return
     */
    @RequiresPermissions("epidemic:writeuser:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WxCpSysUser wxCpSysUser)
    {
        startPage();
        List<WxCpSysUser> list = epidemicWriteDeptService.selectWriteUserList(wxCpSysUser);
        return getDataTable(list);
    }

    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add(ModelMap mmap,String deptId) {
        mmap.put("writeDeptId", deptId);
        return prefix + "/add";
    }

    /**
     * 新增保存用户的关联关系
     */
    @RequiresPermissions("epidemic:writeuser:add")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WxCpSysUserBatch wxCpSysUserBatch,String writeDeptId)
    {
        EpidemicUserDept epidemicUserDept = new EpidemicUserDept();
        epidemicUserDept.setDeptId(Long.parseLong(writeDeptId));
        epidemicUserDept.setRemark(wxCpSysUserBatch.getRemark());
        int insertRow = 0;
        Long userIds[] = wxCpSysUserBatch.getUserIds();
        for (int i = 0; i < userIds.length; i++) {
            epidemicUserDept.setUserId(userIds[i]);
            //新增之前判断，若已存在则不再重复插入
            int k = epidemicWriteDeptService.selectCountByUserIdAndDeptId(userIds[i],epidemicUserDept.getDeptId());
            if(k>0){
                logger.info("当前用户对应的部门已存在,勿重复添加");
            }else{
                int j = epidemicWriteDeptService.insertWriteUserDept(epidemicUserDept);
                if(j>0){
                    insertRow++;
                }else{
                    logger.info("操作失败");
                }
            }
        }
        return toAjax(insertRow);
    }

    /**
     * 移除关联关系【并不删除用户基础信息】
     * @param ids
     * @return
     */
    @RequiresPermissions("epidemic:writeuser:remove")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(epidemicWriteDeptService.deleteWriteUserDeptByIds(ids));
    }

    /**
     * 修改用户关联关系
     */
    @RequiresPermissions("epidemic:writeuser:edit")
    @GetMapping("/edit/{userDeptId}")
    public String edit(@PathVariable("userDeptId") Long userDeptId, ModelMap mmap)
    {
        WxCpSysUser wxCpSysUser = epidemicWriteDeptService.selectWriteUser(userDeptId);
        mmap.put("user", wxCpSysUser);
        return prefix + "/edit";
    }

    /**
     * 修改用户的关联关系
     */
    @RequiresPermissions("epidemic:writeuser:edit")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated WxCpSysUser user)
    {
        EpidemicUserDept epidemicUserDept = new EpidemicUserDept();
        epidemicUserDept.setId(user.getUserDeptId());
        epidemicUserDept.setUserId(Long.parseLong(user.getUserId()));
        epidemicUserDept.setDeptId(user.getDeptId());
        epidemicUserDept.setRemark(user.getRemark());
        return toAjax(epidemicWriteDeptService.updateWriteUserDept(epidemicUserDept));
    }

    /**
     * 叶子节点判断
     * @param deptId
     * @return
     */
    @PostMapping("/yezi")
    @ResponseBody
    public AjaxResult addSave(String deptId) {
        int count  = epidemicControlDeptService.selectDeptCount(Long.parseLong(deptId));
        if (count > 0)        {
            return AjaxResult.warn("存在叶子节点,不允许新增");
        }
        return toAjax(1);
    }
}
